//
//  DiagnosisRecordViewController.m
//  MobileChecker
//
//  Created by Lymons on 15-1-8.
//  Copyright (c) 2015年 Lymons. All rights reserved.
//

#import "DiagnosisRecordViewController.h"
#import "SelfDiagnosisViewModel.h"

@interface DiagnosisRecordViewController ()

@property (strong, nonatomic) IBOutlet UIButton *recordButton;
@property (strong, nonatomic) IBOutlet UILabel *leftSecondsLabel;
@property (strong, nonatomic) IBOutlet UILabel *volumeDbLabel;
@property (strong, nonatomic) IBOutlet UIButton *resultOkButton;
@property (strong, nonatomic) IBOutlet UIButton *resultNgButton;

@property (strong, nonatomic) DiagnosisRecordViewModel *viewModel;

@end

@implementation DiagnosisRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self bindViewModel];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.resultOkButton.enabled = NO;
    self.resultNgButton.enabled = NO;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.viewModel completeTheCheckCommand];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -

- (void)bindViewModel {
    self.viewModel = [[DiagnosisRecordViewModel alloc] init];
    
    self.recordButton.rac_command = self.viewModel.executeCheck;
    [[self.recordButton.rac_command executionSignals] subscribeNext:^(id x) {
        self.resultOkButton.enabled = NO;
        self.resultNgButton.enabled = NO;
    }];
    
    @weakify(self);
    [[RACSignal combineLatest:@[[self.recordButton.rac_command.executing not]]
                       reduce:^(NSNumber *executing1){
                           return @([executing1 boolValue]);
                       }] subscribeNext:^(NSNumber *x) {
                           @strongify(self)
                           self.resultOkButton.enabled = YES;
                           self.resultNgButton.enabled = YES;
                       }];
    
    void (^resultBlock)(BOOL) = ^(BOOL result) {
        NSLog(@"Mic is OK : %@", result ? @"YES" : @"NO");
        @strongify(self);
        [self performSegueWithIdentifier:@"SoundRecordCompleted" sender:self];
    };
    self.resultOkButton.rac_command = [self.viewModel makeCommandWithBlock:resultBlock simple:YES];
    self.resultNgButton.rac_command = [self.viewModel makeCommandWithBlock:resultBlock simple:NO];
    
    RAC(self, recordButton.enabled) = [self.viewModel.executeCheck.executing not];
    RAC(self, volumeDbLabel.hidden) = RACObserve(self, recordButton.enabled);
    RAC(self, volumeDbLabel.text) = RACObserve(self, viewModel.volumeDbString);
    
    RAC(self, leftSecondsLabel.text) = [RACObserve(self.viewModel, leftSeconds) map:^id(NSNumber *value) {
//        @strongify(self);
//        if ([value integerValue] == 0) {
//            self.resultOkButton.enabled = YES;
//            self.resultNgButton.enabled = YES;
//        }
        return [NSString stringWithFormat:@"%@秒", value];
    }];
    
}

@end
